''' <summary>
''' Class to provide very fast importing of delimited text files
''' </summary>
Public Class clsDataEFDC
    Inherits clsDataText

    Private _CalendarYear As Integer = 2000

    Public Overrides Sub Initialize(ByVal _Filename As String)
        mDataType = enumDataType.EFDC
        Open(_Filename)
    End Sub

    Public Property CalendarYear() As Integer
        Get
            Return _CalendarYear
        End Get
        Set(ByVal value As Integer)
            _CalendarYear = value
        End Set
    End Property

    ''' <summary>
    ''' Instantiate and figure out if can use very fast CsvReader class
    ''' </summary>
    ''' <param name="_Filename">Name of text file to read</param>
    Private Sub Open(ByVal _Filename As String)
        mFilename = _Filename
        mRecordNum = -1
        If sr IsNot Nothing Then sr.Close() : sr.Dispose()
        sr = New IO.StreamReader(mFilename, System.Text.Encoding.ASCII, False, cBufferSize)
        mFileLength = sr.BaseStream.Length
        mDelimiters = " "

        'get fieldnames from header (or leave empty)
        'c   I    J    K    TIME       CHL        TOC        PO4        TKN        TP         NH4        NOx        TDP        Chlc      Chld        Chlg       DO                                                                                                               
        '   27   84    1   75.54166  9.973E+00  5.788E+00  5.312E-03  5.318E-01  7.029E-02  2.192E-02  2.610E-01  4.106E-02  3.387E+00  3.297E+00  3.289E+00  1.046E+01

        mFieldNames.Clear()
        mLastLineRead = sr.ReadLine
        Dim ar() As String = mLastLineRead.Split(New Char() {" "}, StringSplitOptions.RemoveEmptyEntries)
        mFieldNames.Add("Station_ID")
        mFieldNames.Add("Date_Time")
        For i As Integer = 5 To ar.Length - 1
            mFieldNames.Add(ar(i))
        Next
        mFieldNames.Add("LEW_Pct")
        mFieldNames.Add("Depth_M")
    End Sub

    ''' <summary>
    ''' Read next line from file into memory
    ''' </summary>
    ''' <returns>True if successful</returns>
    Public Overrides Function Read() As Boolean
        Try
            Do
                If sr.EndOfStream Then Return False

                mLastLineRead = sr.ReadLine
                mItems.Clear()
                Dim ar() As String = mLastLineRead.Split(mDelimiters.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
                If ar.Length < 4 Then Return False
                mItems.Add(String.Format("Lyr={2,2}; I={0,3}; J={1,3}", ar(0), ar(1), ar(2))) 'Station_ID
                Dim dt As New Date(_CalendarYear, 1, 1)
                dt = dt.AddDays(ar(3))
                dt = New Date(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0).AddMinutes(IIf(dt.Second > 30, 1, 0))
                mItems.Add(dt.ToString("MM/dd/yyyy HH:mm:ss"))

                For i As Integer = 4 To ar.Length - 1
                    mItems.Add(ar(i))
                Next
                mItems.Add(ar(0)) 'width is I
                mItems.Add(ar(2)) 'depth is K
            Loop Until Not mActiveStationIDSet OrElse mItems(0) = mActiveStationID
            Return True
        Catch ex As Exception
            ErrorMsg(, ex)
            Return False
        End Try
    End Function

    ''' <summary>
    ''' Call this when the delimiters or header flag may have changed
    ''' </summary>
    ''' <param name="_Delimiters">String containing all delimiters</param>
    ''' <param name="_HasHeaders">True if first line contains column headers</param>
    Public Overrides Sub Reopen(ByVal _Delimiters As String, ByVal _HasHeaders As Boolean)
        Open(mFilename)
    End Sub

    Public Overrides Sub Rewind()
        mRecordNum = -1
        Open(mFilename)
    End Sub

End Class
